package alogrithm;

/**
 * @author lbw
 * @version 1.0
 * @date 2021/10/20 18:32
 */
import java.util.function.IntConsumer;
// you can import any package you need here
// -- write your code here --

public class Solution {
    // you can delcare any attributes here if you need
    // -- write your code here --
    private Object o = new Object();
    private int n;
    public static volatile int count = 1;
    public Solution(int n) {
        // -- write your code here --
        this.n = n;
    }

    public void odd(IntConsumer printNumber) throws InterruptedException {
        // -- write your code here --
        while (count < n) {
            synchronized(this) {
                printNumber.accept(count);
                count++;
                try {
                    if (count % 2 == 0) {
                        wait();
                    }
                } finally {
                    notify();
                }

            }
        }

    }

    public void even(IntConsumer printNumber) throws InterruptedException {
        // -- write your code here --
        while (count < n) {
            synchronized(this) {
                printNumber.accept(count);
                count++;
                try {
                    if (count % 2 == 1) {
                        wait();
                    }
                } finally {
                    notify();
                }
            }
        }
    }
}
